package com.eco.generator;


import com.baomidou.mybatisplus.annotation.*;

import java.lang.reflect.Field;
import java.util.LinkedList;
import java.util.List;

public class SqlPrinter {
    public static void main(String[] args) throws ClassNotFoundException {
        String packagePrefix = "pri.black.garden.domain.";

//        String[] domains = {"Garden","Block","Vegetables"};
        String[] domains = {"PlanLog"};

        StringBuilder sb = new StringBuilder();
        for (String domain : domains) {
            String string = genSql(packagePrefix + domain);
            sb.append(string);
            sb.append("\n");
        }
        System.out.println(sb);
    }

    private static String genSql(String className) throws ClassNotFoundException {
        Class<?> clazz = Class.forName(className);

        TableName annotation1 = clazz.getAnnotation(TableName.class);
        TableName name = annotation1;
        Field[] declaredFields = clazz.getDeclaredFields();

        StringBuilder sb = new StringBuilder();
        sb.append("create table ");
        sb.append(name.value());
        sb.append("(");

        List<Field> avalible = new LinkedList<>();
        for (int i = 0; i < declaredFields.length; i++) {
            Field declaredField = declaredFields[i];
            Class<?> type = declaredField.getType();
            String sqlType = TypeMapper.getSqlType(type);
            if (sqlType == null) {
                System.err.println("not find " + declaredField.getName());
                continue;
            }
            avalible.add(declaredField);
        }

        for (int i = 0; i < avalible.size(); i++) {
            Field declaredField = avalible.get(i);
            Class<?> type = declaredField.getType();
            String sqlType = TypeMapper.getSqlType(type);
            if (sqlType == null) {
                System.err.println("not find " + declaredField.getName());
                continue;

            }

            TableId tableId = declaredField.getAnnotation(TableId.class);
            if (tableId != null) {
                sb.append(declaredField.getName());
                sb.append(" ");
                sb.append(sqlType);
                declaredField.setAccessible(true);
                if (tableId.type() == IdType.AUTO) {
                    sb.append(" ");
                    sb.append("auto_increment primary key");
                }
            } else {
                TableField annotation = declaredField.getAnnotation(TableField.class);
                if (annotation != null) {
                    String value = annotation.value();
                    sb.append(value);
                    sb.append(" ");
                    sb.append(sqlType);
                } else {
                    TableLogic tableLogic = declaredField.getAnnotation(TableLogic.class);
                    if (tableLogic != null) {
                        sb.append(tableLogic.value());
                        sb.append(" ");
                        sb.append(sqlType);
                    } else {
                        sb.append(declaredField.getName());
                        sb.append(" ");
                        sb.append("unknown");
                    }
                }
            }
            if (i == avalible.size() - 1) {
                System.out.println("skip dot");
            }else {
                sb.append(",\n");
            }
        }
        sb.append(");");
        String string = sb.toString();
        return string;
    }
}
